home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2240 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.1 KB

  1. Path: news.mindlink.net!news
  2. From: genew@mindlink.bc.ca (Gene Wirchenko)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: quick decision: is n a power of 2?
  5. Date: Sat, 20 Jan 1996 00:32:12 GMT
  6. Organization: MIND LINK! - British Columbia, Canada
  7. Message-ID: <4dpd94$c25@fountain.mindlink.net>
  8. References: <4dorr8$i58@cloner3.netcom.com> <4dp5dk$t3r@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: line069.nwm.mindlink.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. babycox@aol.com (BabyCox) wrote:
  13.  
  14.  
  15. >>#define ISPOW2(x) (((x) & 1) ^ 1)
  16.  
  17. >That will not work, it returns the next lowest MULTIPLE of two, here's
  18. >what I would do:
  19.  
  20. >Boolean IsPowerOfTwo(long x)
  21. >{
  22. >  for(short y = 0;y<=31;y++)
  23. >  {
  24. >    if (x = 1) return true;
  25. >    x >>= 1;
  26. >  }
  27. >  return false;
  28. >}
  29.  
  30.      This doesn't work!  On the first iteration, x will be assigned
  31. the value 1.  This will make the if true and so the "return true;"
  32. will be done.
  33.  
  34.      If you meant "==", it still doesn't work.  It's a LONG test for
  35. !=0.
  36.  
  37. Sincerely,
  38.  
  39. Gene Wirchenko
  40.  
  41. C Pronunciation Guide:
  42.      y=x++;     "wye equals ex plus plus semicolon"
  43.      x=x++;     "ex equals ex doublecross semicolon"
  44.  
  45.